<?php
/**
 * Created by PhpStorm.
 * User: davin
 * Date: 14-10-19
 * Time: 下午3:26
 */

class BackIndexController extends BaseController {

    static $view_path = 'back_end';
    /**
     * 后台登录
     * @return mixed
     */
    public function doLogin()
    {
        $err_msg = null;
        //登录操作
        if($this->isSubmit()){
            $repo = App::make('UserRepository');
            $input = Input::all();

            if ($repo->login($input)) {

                //判断用户类型
                if(Auth::User()->isStaff())
                    return Redirect::action('BackDashboardController@getIndex');
                if(Auth::User()->isMember())
                    return Redirect::action('MobileDashboardController@getIndex');
            } else {
                if ($repo->isThrottled($input)) {
                    $err_msg = Lang::get('confide::confide.alerts.too_many_attempts');
                } elseif ($repo->existsButNotConfirmed($input)) {
                    $err_msg = Lang::get('confide::confide.alerts.not_confirmed');
                } else {
                    $err_msg = Lang::get('confide::confide.alerts.wrong_credentials');
                }

                return Redirect::action('BackIndexController@doLogin')
                    ->withInput(Input::except('password'))
                    ->with('error', $err_msg);
            }
        }
        return View::make(self::$view_path.'.login');
    }

    /**
     * 后台登出操作
     */
    public function doLogout(){

        Confide::logout();

        return Redirect::action('BackIndexController@doLogin')
            ->with('success', '注销成功!');
    }
}